package com.qs.eggs.account;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.google.gson.Gson;
import com.qs.eggs.R;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.Call;

public class ForgetPasswordActivity extends Activity implements View.OnClickListener{
    private EditText number,yanzhengma;
    private Button fasong,next;
    public static Activity instance;
    private String codeformService="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        instance=this;
        initView();
    }
    private void initView(){
        number=(EditText)findViewById(R.id.et_number);
        yanzhengma=(EditText)findViewById(R.id.et_yanzhengma);
        fasong=(Button)findViewById(R.id.fasong);
        next=(Button)findViewById(R.id.next);

        fasong.setOnClickListener(this);
        next.setOnClickListener(this);
    }
    @Override
    public void onClick(View view){
        switch (view.getId()){
            case R.id.fasong:
                huoqu();
                break;
            case R.id.next:
                if(number.length()==0||yanzhengma.length()==0){
                    Toast.makeText(this,"手机号或验证码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!codeformService.equals("\""+yanzhengma.getText().toString()+"\"")){
                    Toast.makeText(this,"验证码填写不正确",Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent=new Intent(this,ForgetPasswordnextActivity.class);
                startActivity(intent);
                break;
        }
    }
    private void huoqu(){
        final String txt = number.getText().toString();
        if (txt.equals("")) {
            Toast.makeText(this, "请输入手机号！", Toast.LENGTH_SHORT).show();
            return;
        }if(!isMobileNO(txt)){
            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
        }else{
            checkPhone();

        }

    }
    private void checkPhone(){
        HashMap<String, String> map = new HashMap<>();
        map.put("phone",number.getText().toString() );
        Gson gson=new Gson();
        String str=gson.toJson(map);
//        OkHttpUtils
//                .post()
//                .url(Config.url2+"register?action=isExitsPhone")
//                .addParams("data",str)
//                .build()
//                .execute(new StringCallback() {
//                    @Override
//                    public void onError(Call call, Exception e, int id) {
//
//                    }
//                    @Override
//                    public void onResponse(String response, int id) {
//                      System.out.print("=="+response);
//                    if (response.equals("\"true\"")){
//                        Toast.makeText(ForgetPasswordActivity.this, "此手机号还未注册", Toast.LENGTH_LONG).show();
//                    }else{
//                        TimerCount timer = new TimerCount(60000, 1000, fasong);
//                        timer.start();
//                        HashMap<String, String> map = new HashMap<>();
//                        map.put("phone",number.getText().toString() );
//                        Gson gson=new Gson();
//                        String st=gson.toJson(map);
//
//                        OkHttpUtils
//                                .post()
//                                .url(Config.url2+"register?action=getSMSCode")
//                                .addParams("data",st)
//                                .build()
//                                .execute(new StringCallback() {
//                                    @Override
//                                    public void onError(Call call, Exception e, int id) {
//
//                                    }
//                                    @Override
//                                    public void onResponse(String response, int id) {
//                                        System.out.print("验证码=="+response);
//                                        codeformService=response;
//
//                                    }
//                                });
//
//                    }
//                    }
//                });

    }
    private boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("[1][34578]\\d{9}" );

        Matcher m = p.matcher(mobiles);

        return m.matches();

    }
}
